package com.meida.module.bus.provider.service.impl;

import com.google.common.collect.Lists;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.module.bus.client.entity.BusReceiptDeliveryRecordBack;
import com.meida.module.bus.provider.api.soap.SyncSapUtil;
import com.meida.module.bus.provider.api.util.ApiConstants;
import com.meida.module.bus.provider.api.util.PmsApiUtil;
import com.meida.module.bus.provider.mapper.BusReceiptDeliveryRecordBackMapper;
import com.meida.module.bus.provider.service.BusRecDeliBkReqConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 预留发货冲销申请表接口实现类
 *
 * @author flyme
 * @date 2020-12-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusRecDeliBkReqConfServiceImpl extends BaseServiceImpl<BusReceiptDeliveryRecordBackMapper, BusReceiptDeliveryRecordBack> implements BusRecDeliBkReqConfService {

    @Autowired
    private PmsApiUtil pmsApiUtil;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusReceiptDeliveryRecordBack brdrb, EntityMap extra) {
        return ResultBody.ok();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusReceiptDeliveryRecordBack> cq, BusReceiptDeliveryRecordBack brdrb, EntityMap requestMap) {
//        cq.notIn("syncStatus", ApiConstants.REC_DELIVERY_CANCEL);
        cq.in("syncStatus", ApiConstants.REC_BK_DELIVERY_INT,ApiConstants.REC_BK_DELIVERY_ERR);
        cq.like(BusReceiptDeliveryRecordBack.class, "status");
        cq.like(BusReceiptDeliveryRecordBack.class, "projectCode");
        cq.like(BusReceiptDeliveryRecordBack.class, "projectName");
        cq.like(BusReceiptDeliveryRecordBack.class, "majorCode");
        cq.like(BusReceiptDeliveryRecordBack.class, "majorName");
        cq.like(BusReceiptDeliveryRecordBack.class, "packageNo");
        cq.like(BusReceiptDeliveryRecordBack.class, "packageName");
        cq.like(BusReceiptDeliveryRecordBack.class, "userName");
        cq.like(BusReceiptDeliveryRecordBack.class, "appTime");
        cq.like(BusReceiptDeliveryRecordBack.class, "receivedDate");
        cq.like(BusReceiptDeliveryRecordBack.class, "zid");
        cq.like(BusReceiptDeliveryRecordBack.class, "zitem");
        cq.like(BusReceiptDeliveryRecordBack.class, "budat");
        cq.like(BusReceiptDeliveryRecordBack.class, "bldat");
        cq.like(BusReceiptDeliveryRecordBack.class, "bktxt");
        cq.like(BusReceiptDeliveryRecordBack.class, "supplierCode");
        cq.like(BusReceiptDeliveryRecordBack.class, "supplierName");
        cq.like(BusReceiptDeliveryRecordBack.class, "reserveNo");
        cq.like(BusReceiptDeliveryRecordBack.class, "reserveRow");
        cq.like(BusReceiptDeliveryRecordBack.class, "lgort");
        cq.like(BusReceiptDeliveryRecordBack.class, "ext01");
        cq.like(BusReceiptDeliveryRecordBack.class, "msgtyFromSap");
        cq.like(BusReceiptDeliveryRecordBack.class, "msgtxFromSap");
        cq.like(BusReceiptDeliveryRecordBack.class, "mblnrFromSap");
        cq.like(BusReceiptDeliveryRecordBack.class, "mblppoFromSap");
        cq.like(BusReceiptDeliveryRecordBack.class, "mjahrFromSap");

        List<String> majorCodes = pmsApiUtil.getAuthMajorCodes(OpenHelper.getUserId());
        if(null != majorCodes) {
            cq.in("majorCode", majorCodes);
        }
        List<String> projectCodeLists = pmsApiUtil.getAuthProjectIds(OpenHelper.getUserId());
        if(null != projectCodeLists) {
            cq.in("projectCode", projectCodeLists);
        }
        cq.orderByDesc("brdrb.createTime");
        return ResultBody.ok();
    }

    @Override
    public void syncSap(List<BusReceiptDeliveryRecordBack> records) throws Exception {
        if (pmsApiUtil.sapConnectionForRecCheck()) {
            return;
        }
        Map<String, String> rsMap = SyncSapUtil.syncSapForCharOff(records);
        records.stream().forEach(recordObj -> {
            if (ApiConstants.REC_DERVERY_SAP_BACK_ERR.equals(rsMap.get(ApiConstants.MAP_MSGTYFROMSAP))) {
                recordObj.setSyncStatus(ApiConstants.REC_DELIVERY_ERR);
                recordObj.setMsgtyFromSap(ApiConstants.REC_DERVERY_SAP_BACK_ERR);
                recordObj.setMsgtxFromSap(rsMap.get(ApiConstants.MAP_MSGTXFROMSAP));
            } else {
                recordObj.setSyncStatus(ApiConstants.REC_DELIVERY_FINISH);
            }
        });
    }

    @Override
    public void reqBkCancel(Long[] selectIds) {
        List<Long> selectIdsList = Arrays.asList(selectIds);
        List<BusReceiptDeliveryRecordBack> recordCancels = Lists.newArrayList();
        Collection<BusReceiptDeliveryRecordBack> busReceiptDeliveryRecordBacks = this.listByIds(selectIdsList);
        busReceiptDeliveryRecordBacks.stream().forEach(updObj -> {
            updObj.setSyncStatus(ApiConstants.RREC_BK_DELIVERY_CANCEL);
            updObj.setDelFlg(ApiConstants.DEL_ON);
            recordCancels.add(updObj);
        });
        if (0 < recordCancels.size()) {
            this.updateBatchById(recordCancels);
        }
    }
}
